package edu.stu.mall.activity.tab

import android.graphics.BitmapFactory
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import edu.stu.mall.BR
import edu.stu.mall.R
import edu.stu.mall.databinding.ActivityTabBinding
import edu.stu.mall.fragment.home.FragHome
import edu.stu.mall.fragment.hot_key.FragHotKey
import edu.stu.mall.fragment.knowledge.FragKnowledge
import edu.stu.mall.fragment.personal.FragPersonal
import edu.mall.base.BaseActivity
import edu.mall.base.adapter.Pager2Adapter
import edu.mall.base.tab.NavigationBottomBar

class TabActivity : BaseActivity<ActivityTabBinding, TabViewModel>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_tab
    }

    override fun getViewModelId(): Int {

        return BR.tabVm
    }

    override fun initViewData() {

        // 初始化ViewPager2
        initPages()
        // 选中图片的数组
        val icons = arrayOf(
            BitmapFactory.decodeResource(resources, R.drawable.icon_home_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_hot_key_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_knowledge_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_personal_selected),
        )
        // 未选中图片的数组
        val iconGreys = arrayOf(
            BitmapFactory.decodeResource(resources, R.drawable.icon_home_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_hot_key_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_knowledge_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_personal_grey),
        )

        val tabText= arrayOf("首页","热点","体系","个人")

        binding?.tabBottomBar?.setSelectedIcons(icons.toList())
        binding?.tabBottomBar?.setUnselectIcons(iconGreys.toList())
        binding?.tabBottomBar?.setTabText(tabText.toList())
        binding?.tabBottomBar?.setupViewpager(binding?.tabViewPage2)
        binding?.tabBottomBar?.start()


        binding?.tabBottomBar?.registerTabClickListener(object : NavigationBottomBar.OnBottomTabClickListener{
            override fun tabClick(position: Int) {
                Log.e("tab切换", "tabClick: $position", )
            }

        })


    }
    private fun initPages(){
        val tabPages= mutableListOf<Fragment>()
        tabPages.add(FragHome())
        tabPages.add(FragHotKey())
        tabPages.add(FragKnowledge())
        tabPages.add(FragPersonal())

        val pager2Adapter= Pager2Adapter(this)
        pager2Adapter.setData(tabPages)
        binding?.tabViewPage2?.offscreenPageLimit= ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        binding?.tabViewPage2?.adapter=pager2Adapter
    }

}